/* --- Load Hojoki API und try to login --- */
var authWithHojoki = function(auth) {
	// first: authenticate (needs: Hojoki API)
	haed.loadJS("hojoki/api/hojoki.api.v1.js").done(function(){
		
		debug("[myHojokiApp] Hojoki API loaded. Trying to login ..");
		
		// authenticate
		jQuery.ajax("http://alpha.hojoki.com/api/hojoki/v1/loginBasic", {
			headers: {
				Authorization: auth
			}
		}).fail(function() { // Login failed
			debug("[myHojokiApp] .. failed.");
			window.parentSandboxBridge.checkLogin(false);
		}).done(function(){ // Login successful
			debug("[myHojokiApp] .. successful.");
			getHojokiActivities();
			window.parentSandboxBridge.checkLogin(true);
		});
	});
};

/* --- Logout --- */
var doLogout = function() {
	clearInterval(pingInterval);
	enact.serviceMgr.logout();
};

/* --- subscribe to activity notifications --- */
var usedActivityURIs = new Array();
var getHojokiActivities = function(){
	// authentication is done, load activity-API, renderer-engine and default Hojoki resources (text, images)
	jQuery.when(
		haed.loadJS("hojoki/activity/hojoki.activityMgr.js", "hojoki/hojoki.serviceMgr.js", "enact/ui/enact.ui.semdata.factory.js"),
		haed.loadResource("hojoki"))
	.then(function(){
		
		debug("[myHojokiApp] Activity API, Renderer-Engine and default Hojoki Resources loaded.");
		
		// constantly ping Hojoki in order to prevent idle
		pingHojoki();
		
		// create a callback function for rendering incoming activities (using Hojoki semantic data rendering API)..
		var callback = function(activity){
			var activityHTML = enact.ui.semdata.factory.render(null, "http://schema.hojoki.com/eas/1.0/activity", activity.model, activity.uri).html();
			debug("[myHojokiApp] Received Activity " + activity.uri);
			if(activityHTML.search(/data-person-uri="http:/) > -1) { // avoids semi-empty activity notifications
				if( !(usedActivityURIs.indexOf(activity.uri) > -1) ) { // avoids showing the same activity notification multiple times
					usedActivityURIs.push(activity.uri);
					debug("[myHojokiApp] Pushed Activity " + activity.uri + " to main application.");
					activityHTML = activityHTML.replace(/src="\/static/, 'src="http://alpha.hojoki.com/static');
					pushActivity(activityHTML);
				}				
			}
		};
		enact.serviceMgr.getContexts({
			success: function(contexts) {
				
				for(var i=0, len=contexts.length; i<len; i++) {
					debug("[myHojokiApp] Registering for " + contexts[i].name + " | " + contexts[i].id);
					hojoki.activityMgr.register4IncomingActivities(contexts[i].id, callback );
					
					// Done, now we are live-connected to specified group, 
					// any incoming activity of this group will be instantly appear in this web app..
				}
				
			}
		});
	});
};

var pingInterval;
var pingHojoki = function() {
		
	pingInterval = setInterval(function(){	
		hojoki.serviceMgr.pingStatus("ping", {
			error: function() { debug("[myHojokiApp] Ping failed."); },
			success: function() { debug("[myHojokiApp] Ping done."); }
		});
	}, 20000);
	
	hojoki.serviceMgr.subscribe2PingStatus({
		success: function(status) {
		  debug("[myHojokiApp] Ping notification: " + status);
		}
	});
};


/* --- use child sandbox bridge to hand data over to main application --- */
exposed = {};
exposed.authWithHojoki = function(auth) { authWithHojoki(auth);	};
exposed.doLogout = function() { doLogout(); };
window.childSandboxBridge = exposed;
/* --- tell parent that child is finished with loading --- */
window.parentSandboxBridge.ready();
/* --- use parent sandbox bridge to write debug statements to console --- */
var debug = function(msg) { window.parentSandboxBridge.debug(msg); };
var pushActivity = function(activityHTML) { window.parentSandboxBridge.pushActivity(activityHTML); };
